package hyj.qx.websocket.service.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.stereotype.Component;

@Component
public class MessageBroadcaster {
    private static final String CHANNEL = "chat_channel";

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 发送消息到频道
    public void broadcast(String message) {
        redisTemplate.convertAndSend(CHANNEL, message);
    }

    // 消息监听容器（需配置）
    @Bean
    public MessageListenerAdapter listenerAdapter() {
        return new MessageListenerAdapter(new MessageListener() {
            @Override
            public void onMessage(Message message, byte[] pattern) {
                String msg = new String(message.getBody());
                // 实际处理逻辑...
            }
        });
    }
}